home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 June / PCWorld_2007-06_cd.bin / v cisle / tclock / tclocklight-040702-3.exe / source / sntp / main.c < prev    next >
C/C++ Source or Header  |  2004-05-10  |  7KB  |  279 lines

  1. /*-------------------------------------------------------------
  2.   main.c : Time Synchronization
  3.   (C) Kazuto Sato 1997-2003
  4.   For the license, please read readme.txt.
  5.   
  6.   Written by Kazubon, Nanashi-san
  7. ---------------------------------------------------------------*/
  8.  
  9. #include "tcsntp.h"
  10.  
  11. /* Globals */
  12.  
  13. BOOL ExecCommandString(HWND hwnd, const char *command);
  14.  
  15. HINSTANCE g_hInst;                 // instance handle
  16. char      g_mydir[MAX_PATH];       // path to this exe file
  17. BOOL      g_bIniSetting = FALSE;   // save setting to ini file?
  18. char      g_inifile[MAX_PATH];     // ini file name
  19. char      g_langfile[MAX_PATH];    // tclang.txt
  20. HFONT     g_hfontDialog = NULL;    // dialog font
  21. HWND      g_hwndMain  = NULL;      // main window
  22. HICON     g_hIconPlay, g_hIconStop;
  23.                                    // icons to use frequently
  24. /* Statics */
  25.  
  26. static int TCSNTPMain(void);
  27. static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  28.     WPARAM wParam, LPARAM lParam);
  29. static void InitApp(void);
  30. static void OnCreate(HWND hwnd);
  31. static void OnDestroy(HWND hwnd);
  32. static void CheckCommandLine(HWND hwnd, BOOL bPrev);
  33.  
  34. /*-------------------------------------------
  35.   WinMain
  36. ---------------------------------------------*/
  37. #ifdef NODEFAULTLIB
  38. void WINAPI WinMainCRTStartup(void)
  39. {
  40.     g_hInst = GetModuleHandle(NULL);
  41.     ExitProcess(TCSNTPMain());
  42. }
  43. #else
  44. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  45.     LPSTR lpCmdLine, int nCmdShow)
  46. {
  47.     g_hInst = hInstance;
  48.     return TCSNTPMain();
  49. }
  50. #endif
  51.  
  52. /*-------------------------------------------
  53.   main routine
  54. ---------------------------------------------*/
  55. int TCSNTPMain(void)
  56. {
  57.     MSG msg;
  58.     WNDCLASS wndclass;
  59.     HWND hwnd;
  60.     
  61.     // not to execute the program twice
  62.     hwnd = FindWindow(CLASS_TCLOCKSNTP, NULL);
  63.     if(hwnd != NULL)
  64.     {
  65.         CheckCommandLine(hwnd, TRUE);
  66.         return 1;
  67.     }
  68.     
  69.     InitApp();
  70.     
  71.     // register a window class
  72.     wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  73.     wndclass.lpfnWndProc   = WndProc;
  74.     wndclass.cbClsExtra    = 0;
  75.     wndclass.cbWndExtra    = 0;
  76.     wndclass.hInstance     = g_hInst;
  77.     wndclass.hIcon         = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_TCLOCK));
  78.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  79.     wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  80.     wndclass.lpszMenuName  = NULL;
  81.     wndclass.lpszClassName = CLASS_TCLOCKSNTP;
  82.     RegisterClass(&wndclass);
  83.     
  84.     // create a hidden window
  85.     g_hwndMain = CreateWindowEx(0,
  86.         CLASS_TCLOCKSNTP, "TClock SNTP", WS_OVERLAPPEDWINDOW,
  87.         CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
  88.         NULL, NULL, g_hInst, NULL);
  89.     // ShowWindow(g_hwndMain, SW_SHOW);
  90.     // UpdateWindow(g_hwndMain);
  91.     
  92.     while(GetMessage(&msg, NULL, 0, 0))
  93.     {
  94.         if(g_hDlg && IsWindow(g_hDlg) && IsDialogMessage(g_hDlg, &msg)) ;
  95.         else
  96.         {
  97.             TranslateMessage(&msg);
  98.             DispatchMessage(&msg);
  99.         }
  100.     }
  101.     
  102.     return msg.wParam;
  103. }
  104.  
  105. /*-------------------------------------------
  106.   initialize
  107. ---------------------------------------------*/
  108. void InitApp(void)
  109. {
  110.     GetModuleFileName(g_hInst, g_mydir, MAX_PATH);
  111.     del_title(g_mydir);
  112.     strcpy(g_inifile, g_mydir);
  113.     add_title(g_inifile, "tclock.ini");
  114.     g_bIniSetting = TRUE;
  115.     
  116.     // common/langcode.c
  117.     FindFileWithLangCode(g_langfile, GetUserDefaultLangID(), TCLANGTXT);
  118.     g_hfontDialog = CreateDialogFont();
  119.     
  120.     g_hIconPlay = LoadImage(g_hInst, MAKEINTRESOURCE(IDI_PLAY), IMAGE_ICON,
  121.         16, 16, LR_DEFAULTCOLOR|LR_SHARED);
  122.     g_hIconStop = LoadImage(g_hInst, MAKEINTRESOURCE(IDI_STOP), IMAGE_ICON,
  123.         16, 16, LR_DEFAULTCOLOR|LR_SHARED);
  124. }
  125.  
  126. /*-------------------------------------------
  127.   window procedure
  128. ---------------------------------------------*/
  129. LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  130.     WPARAM wParam, LPARAM lParam)
  131. {
  132.     switch (message)
  133.     {
  134.         case WM_CREATE:
  135.             OnCreate(hwnd);
  136.             return 0;
  137.         case WM_DESTROY:
  138.             OnDestroy(hwnd);
  139.             return 0;
  140.         case WM_TIMER:
  141.             switch (wParam)
  142.             {
  143.                 case IDTIMER_MAIN:
  144.                     OnTimerSNTP(hwnd);
  145.                     break;
  146.             }
  147.             return 0;
  148.         // show dialog box
  149.         case SNTPM_SHOWDLG:
  150.             OnShowDialog(hwnd);
  151.             return 0;
  152.         
  153.         case WSOCK_GETHOST: // get IP address
  154.             OnGetHost(hwnd, wParam, lParam);
  155.             return 0;
  156.         case WSOCK_SELECT:  // receive SNTP data
  157.             OnReceive(hwnd, wParam, lParam);
  158.             return 0;
  159.         case SNTPM_SUCCESS:
  160.         {
  161.             HWND hwndClock = GetClockWindow();
  162.             if(hwndClock) InvalidateRect(hwndClock, NULL, FALSE);
  163.         }
  164.         case SNTPM_ERROR:
  165.             if(!g_hDlg)
  166.                 PostMessage(hwnd, WM_CLOSE, 0, 0);
  167.             return 0;
  168.     }
  169.     return DefWindowProc(hwnd, message, wParam, lParam);
  170. }
  171.  
  172. /*-------------------------------------------------------
  173.   WM_CREATE message
  174. ---------------------------------------------------------*/
  175. void OnCreate(HWND hwnd)
  176. {
  177.     if(InitSNTP(hwnd) == FALSE) // sntp.c
  178.     {
  179.         PostMessage(hwnd, WM_CLOSE, 0, 0);
  180.         return;
  181.     }
  182.     
  183.     CheckCommandLine(hwnd, FALSE);
  184.     
  185.     SetTimer(hwnd, IDTIMER_MAIN, 1000, NULL);
  186. }
  187.  
  188. /*-------------------------------------------------------
  189.   WM_DESTROY message
  190. ---------------------------------------------------------*/
  191. void OnDestroy(HWND hwnd)
  192. {
  193.     EndSNTP(hwnd); // sntp.c
  194.     
  195.     KillTimer(hwnd, IDTIMER_MAIN);
  196.     
  197.     if(g_hfontDialog) DeleteObject(g_hfontDialog);
  198.     
  199.     PostQuitMessage(0);
  200. }
  201.  
  202. /*-------------------------------------------
  203.    process command line option
  204. ---------------------------------------------*/
  205. void CheckCommandLine(HWND hwnd, BOOL bPrev)
  206. {
  207.     char name[20], value[20];
  208.     char *p;
  209.     int i;
  210.     BOOL bquot = FALSE;
  211.     BOOL bSilent = FALSE, bOnlyRas = FALSE;
  212.     
  213.     p = GetCommandLine();
  214.     
  215.     while(*p == ' ') p++;
  216.     if(*p == '\"') { bquot = TRUE; p++; }
  217.     while(*p)
  218.     {
  219.         if(bquot) { if(*p == '\"') { p++; break; } }
  220.         else if(*p == ' ') break;
  221.         p++;
  222.     }
  223.     while(*p == ' ') p++;
  224.     
  225.     while(*p)
  226.     {
  227.         if(*p == '/')
  228.         {
  229.             p++;
  230.             for(i = 0; *p && *p != ' ' && i < 19; i++)
  231.             {
  232.                 name[i] = *p++;
  233.             }
  234.             name[i] = 0;
  235.             while(*p == ' ') p++;
  236.             
  237.             value[0] = 0;
  238.             if(*p && *p != '/')
  239.             {
  240.                 for(i = 0; *p && i < 19; i++)
  241.                     value[i] = *p++;
  242.                 value[i] = 0;
  243.             }
  244.             
  245.             if(strcmp(name, "silent") == 0)
  246.             {
  247.                 if(!bPrev) bSilent = TRUE;
  248.             }
  249.             else if(strcmp(name, "ras") == 0)
  250.             {
  251.                 if(!bPrev) bOnlyRas = TRUE;
  252.             }
  253.         }
  254.         else p++;
  255.     }
  256.     
  257.     if(bSilent)
  258.     {
  259.         if(StartSyncTime(hwnd, NULL, bOnlyRas) == FALSE)
  260.             PostMessage(hwnd, WM_CLOSE, 0, 0);
  261.     }
  262.     else
  263.         PostMessage(hwnd, SNTPM_SHOWDLG, 0, 0);
  264. }
  265.  
  266. /* -------------------- Utilities ---------------------------------------*/
  267.  
  268. /*-------------------------------------------
  269.   called in PlayFile function
  270. ---------------------------------------------*/
  271. BOOL ExecCommandString(HWND hwnd, const char *command)
  272. {
  273.     SendStringToOther(GetTClockMainWindow(), hwnd, command,
  274.         COPYDATA_EXEC);
  275.     
  276.     return FALSE;
  277. }
  278.  
  279.